package com.biz.hunter.webchat.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.biz.hunter.dao.CityDAO;
import com.biz.hunter.dao.CompanyDAO;
import com.biz.hunter.dao.DomainDAO;
import com.biz.hunter.dao.EmployeeDAO;
import com.biz.hunter.dao.JobDAO;
import com.biz.hunter.dao.JobTitleDAO;
import com.biz.hunter.dao.SurveyPersonDAO;
import com.biz.hunter.dao.UserDAO;
import com.biz.hunter.db.entity.City;
import com.biz.hunter.db.entity.Company;
import com.biz.hunter.db.entity.Domain;
import com.biz.hunter.db.entity.Employee;
import com.biz.hunter.db.entity.JobDesc;
import com.biz.hunter.db.entity.UserInfo;
import com.biz.hunter.db.entity.jsConfig;
import com.biz.hunter.service.IEmployeeService;
import com.biz.hunter.service.IJobDescService;
import com.biz.hunter.service.impl.SysConfigService;
import com.biz.system.config.WebchatServiceConstant;
import com.biz.wechat.annotation.OAuthRequired;
import com.biz.wechat.util.DateUtil;
import com.biz.wechat.util.SignUtil;

@Controller
@RequestMapping("siemens")
public class SiemensController {

	private static final Logger logger = LoggerFactory
			.getLogger(SiemensController.class);
	@Autowired
	private EmployeeDAO employeeDAO;

	@Autowired
	private IJobDescService jobService;

	@Autowired
	private UserDAO userDAO;

	@Autowired
	private IEmployeeService empService;

	@Autowired
	SurveyPersonDAO surveyPersonDAO;
	
	@Autowired
	private SysConfigService sysConfigService;
	
	// ======>西门子职位热招
	@RequestMapping(value = "job/listHotJobs.page", method = RequestMethod.GET)
	public String viewHotJob(HttpServletRequest request, ModelMap model) {

		int offset;
		int companyId = 959;
		String startStr = request.getParameter("offset");
		if (StringUtils.isEmpty(startStr)) {
			offset = 0;
		} else {
			offset = Integer.valueOf(startStr);
		}

		Company company = this.companyDAO.getById(companyId);
		List<JobDesc> jds = jobDAO.getAllperPage(companyId, offset, 10);
		convertJobDTO(jds);


		model.addAttribute("jobs", jds);
		model.addAttribute("company", company);

		// config
		String ticket = sysConfigService.getJsTicket();
		logger.debug("js api ticket is "+ticket);
		String url = request.getRequestURL().toString();
		if (!StringUtils.isEmpty(request.getQueryString())) {  
            url = url + "?" + request.getQueryString();  
        } 
		logger.debug("request url is " + url);
		
		Map<String, String> jsconfig = SignUtil.getJSSignature(ticket, url);
		
		jsConfig config = new jsConfig();
		config.setAppid(WebchatServiceConstant.APPID);
		config.setCurrenttimestamp(jsconfig.get("timestamp"));
		config.setNonceStr(jsconfig.get("nonceStr"));
		config.setSignatureStr(jsconfig.get("signature"));
		
		model.addAttribute("config", config);

		return "siemens/hotJobList";
	}

	// hotJob更多相关职位
	@RequestMapping(value = "hotJob/more", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> moreHotJobs(HttpServletRequest request,
			int companyId, ModelMap model) {

		Map<String, Object> returnMap = new HashMap<String, Object>();

		int offset = 0;
		String startStr = request.getParameter("offset");
		if (StringUtils.isEmpty(startStr)) {
			offset = 0;
		} else {
			offset = Integer.valueOf(startStr);
		}

		List<JobDesc> jobs = jobDAO.getAllperPage(companyId, offset, 10);
		logger.debug("jobs list is " + jobs);
		this.convertJobDTO(jobs);
		returnMap.put("errorcode", 0);
		returnMap.put("msg", "成功获取数据");
		returnMap.put("jobs", jobs);
		/* config */
		String ticket = sysConfigService.getJsTicket();
		logger.debug("js api ticket is "+ticket);
		String url = request.getRequestURL().toString();
		if (!StringUtils.isEmpty(request.getQueryString())) {  
            url = url + "?" + request.getQueryString();  
        } 
		logger.debug("request url is " + url);
		
		Map<String, String> jsconfig = SignUtil.getJSSignature(ticket, url);
		
		jsConfig config = new jsConfig();
		config.setAppid(WebchatServiceConstant.APPID);
		config.setCurrenttimestamp(jsconfig.get("timestamp"));
		config.setNonceStr(jsconfig.get("nonceStr"));
		config.setSignatureStr(jsconfig.get("signature"));
		
		model.addAttribute("config", config);
		/**/
		return returnMap;
	}

	// 查看职位详情
	@RequestMapping(value = "job/viewDetail.page", method = RequestMethod.GET)
	public String viewJobDetail(HttpServletRequest request, int jobId,
			ModelMap model) {

		JobDesc job = jobDAO.getById(jobId);
		int companyId = job.getCompanyId();
		Company company = companyDAO.getById(job.getCompanyId());

		int cityId = job.getCityId();
		City city = cityDAO.getById(cityId);
		model.addAttribute("job", job);
		model.addAttribute("company", company);

		job.setCity(city);
		job.setCompanyName(company.getName());
		long data = job.getExpiredDate();
		job.setExpiredDateStr(DateUtil.getDateStr(data));
		model.addAttribute("job", job);
		/* config */
		String ticket = sysConfigService.getJsTicket();
		logger.debug("js api ticket is "+ticket);
		String url = request.getRequestURL().toString();
		if (!StringUtils.isEmpty(request.getQueryString())) {  
            url = url + "?" + request.getQueryString();  
        } 
		logger.debug("request url is " + url);
		
		Map<String, String> jsconfig = SignUtil.getJSSignature(ticket, url);
		
		jsConfig config = new jsConfig();
		config.setAppid(WebchatServiceConstant.APPID);
		config.setCurrenttimestamp(jsconfig.get("timestamp"));
		config.setNonceStr(jsconfig.get("nonceStr"));
		config.setSignatureStr(jsconfig.get("signature"));
		
		model.addAttribute("config", config);
		/**/
		return "siemens/zhiweixinxi";
	}

	// 我要自荐
	@RequestMapping(value = "job/selfRecommend.page", method = RequestMethod.GET)
	public String toSelfRecommend(HttpServletRequest request, ModelMap model) {
		HttpSession session = request.getSession();

	    String openid = (String)session.getAttribute("OpenId");
	    logger.debug("openid is " + openid);
	    if (!StringUtils.isEmpty(openid)) {
	      model.addAttribute("openid", openid);
	    }
		return "siemens/selfhouxuanren";
	}
	// 我要自荐
	@RequestMapping(value = "job/otherRecommend.page", method = RequestMethod.GET)
	public String toOtherRecommend(HttpServletRequest request, ModelMap model) {
		HttpSession session = request.getSession();

	    String openid = (String)session.getAttribute("OpenId");
	    logger.debug("openid is " + openid);
	    if (!StringUtils.isEmpty(openid)) {
	      model.addAttribute("openid", openid);
	    }
		return "siemens/houxuanren";
	}
	// 职位分享页面
	@RequestMapping(value = "job/sharejob.page", method = RequestMethod.GET)
	public String sharejobPage(HttpServletRequest request, int jobid,
			ModelMap model) {

		HttpSession session = request.getSession();

		String openid = (String) session.getAttribute("OpenId");
		logger.debug("openid is " + openid);
		if (!StringUtils.isEmpty(openid)) {

			model.addAttribute("openid", openid);

			UserInfo user = userDAO.getByOpenId(openid);
			if (user != null) {

				Employee emp = empService.getByOpenId(openid);
				if (emp != null) {
					model.addAttribute("emp", emp);
				}

				JobDesc job = jobService.searchJobByID(jobid);
				if (job == null) {
					logger.info("invalid Job id " + jobid);
					model.addAttribute("msg", "该职位不存在或者已经过期。");
				}

				int companyId = job.getCompanyId();
				int cityId = job.getCityId();
				Company company = companyDAO.getById(companyId);
				City city = cityDAO.getById(cityId);

				Company expCompany = companyDAO.getById(job
						.getExpectcompanyid());

				job.setCity(city);
				job.setCompanyName(company.getName());
				long data = job.getExpiredDate();
				job.setExpiredDateStr(DateUtil.getDateStr(data));
				model.addAttribute("job", job);
				model.addAttribute("expcompany", expCompany);

				List<Company> companies = companyDAO.getALL();
				model.addAttribute("companies", companies);
			}

		}
		/* config */
		String ticket = sysConfigService.getJsTicket();
		logger.debug("js api ticket is "+ticket);
		String url = request.getRequestURL().toString();
		if (!StringUtils.isEmpty(request.getQueryString())) {  
            url = url + "?" + request.getQueryString();  
        } 
		logger.debug("request url is " + url);
		
		Map<String, String> jsconfig = SignUtil.getJSSignature(ticket, url);
		
		jsConfig config = new jsConfig();
		config.setAppid(WebchatServiceConstant.APPID);
		config.setCurrenttimestamp(jsconfig.get("timestamp"));
		config.setNonceStr(jsconfig.get("nonceStr"));
		config.setSignatureStr(jsconfig.get("signature"));
		
		model.addAttribute("config", config);
		/**/
		return "siemens/sharejobpage";
	}

	private void convertJobDTO(List<JobDesc> jds) {
		for (JobDesc job : jds) {
			int companyId = job.getCompanyId();
			Company company = companyDAO.getById(companyId);

			int cityId = job.getCityId();
			City city = cityDAO.getById(cityId);

			job.setCity(city);
			if (company != null) {
				job.setCompanyName(company.getName());
			}

			long data = job.getExpiredDate();
			job.setExpiredDateStr(DateUtil.getDateStr(data));
		}
	}
	

	@Autowired
	private DomainDAO domainDAO;
	@Autowired
	private JobTitleDAO roleDAO;
	@Autowired
	private JobDAO jobDAO;

	@Autowired
	private CompanyDAO companyDAO;
	@Autowired
	CityDAO cityDAO;
}
